#!/usr/bin/env python

# Volturnus Address Book v2 svn

# -----------------------------------------------------------------------
# Copyright (C) 2008 Bryan Smith

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# -----------------------------------------------------------------------

import sqlite3
import os.path as filecheck

# Create DB Class.
# This class is responsible for the creation of the SQLite database that will store the contact info.
# Values manageed in the database: Last Name, First Name, Street, Town, Province, Postal Code, Country,
# Home Phone Number and Work Phone Number
class Createdb:

    # Constructor - print out message about what this script does and pose question
    def __init__(self):
        print "Volturnus Database Setup"
        print "========================"
        print "This tool will create the Volturnus database."
        print "Are you sure you want to continue?"

    # Gets input from the user - do you want to create a database or not?
    def getInput(self):
        opt = raw_input("y/n: ")
        if opt.lower() == "y":
            self.create()
        elif opt.lower() == "n":
            print "Database not created"
        else:
            print "Unrecognized option"
            self.getInput()

    # Create the database
    def create(self):
        # If db exists, alert the user as there is no need to create a new one - if the db is needed, remove it or rename it
        if filecheck.exists('volturnus.db'): # http://forums.devshed.com/python-programming-11/how-to-see-if-file-exists-83744.html - thanks!
            print "Database already exists. Please move it or delete it to create a new one."
        else:   
            print "Creating database..."
            try:
                # Connect to the db (creating one in the process if required)
                self.con = sqlite3.connect('volturnus.db')
                self.cur = self.con.cursor()
                # Create table with rows for last name, first name, street, town, province/state,
                # postal/zip code, country and phone number
                # All are created as text to account for things like dashes and any extra
                # characters thought to be appropriate
                self.cur.execute('''
                create table voldb (
                    lname   TEXT,
                    fname   TEXT,
                    street TEXT,
                    city_town   TEXT,
                    province_state TEXT,
                    postal_zip TEXT,
                    country TEXT,
                    homephone  TEXT,
                    workphone  TEXT
                )
                ''') # Create the table that stores the data in the newly created database
                self.con.commit() # Commit the changes
                self.con.close() # Close the connection
            except StandardError:
                print "Database creation error. Check that you have write permissions to this directory."
        useless = raw_input("Press enter to continue...")

if __name__ == "__main__":
    c = Createdb()
    c.getInput()
